package com.czl.elm.dao;

import com.czl.elm.domain.Food;
import com.czl.elm.domain.FoodMenu;
import com.czl.elm.domain.Search;
import com.czl.elm.domain.ShopPreferential;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.*;

@Mapper
@Repository
public class SearchMapper {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 测试专用，添加商店的优惠
     * @param shopPreferential
     */
    public void t_addShopPreferential(ShopPreferential shopPreferential) {
        mongoTemplate.save(shopPreferential);
    }

    /**
     * 查找搜索框的查询历史记录
     * @param phoneName
     * @return
     */
    public List<String> searchShopHistory(String phoneName){
        Query query=new Query(Criteria.where("phoneName").is(phoneName));
        Search mgt =  mongoTemplate.findOne(query , Search.class);
        if(mgt==null)return new LinkedList<>();
        return mgt.getSearchList();

    }

    /**
     * 新增搜索框的查询历史记录
     * @param phoneName
     * @param req
     */
    public void insertShopHistory(String phoneName, List<String> req) {
        Search search=new Search(phoneName,req);
        mongoTemplate.save(search);

    }

    /**
     * 删除搜索框的查询历史记录
     * @param phoneName
     */
    public void delShopHistory(String phoneName) {
        Query query=new Query(Criteria.where("phoneName").is(phoneName));
        mongoTemplate.remove(query,Search.class);
    }

    /**
     * 查询一个商店menu菜单栏
     * @param shopid
     * @return
     */
    public Map<String, List<String>> searchMenu(Integer shopid){
        Criteria criteria=new Criteria();
        criteria.andOperator(
                Criteria.where("shopid").is(shopid)

        );

        Query query=new Query();
        query.addCriteria(criteria);
        FoodMenu mgt =  mongoTemplate.findOne(query , FoodMenu.class);
        if(mgt==null)return new HashMap<>();
        return mgt.getShopMap();

    }

    /**
     * 搜索商店所有的菜品清单
     * @param shopid
     * @return
     */
    public List<Food> searchAllFoodByShop(Integer shopid) {

//        LookupOperation lookup=LookupOperation.newLookup().
//                from("sellfood").  //关联从表名
//                localField("_id").     //主表关联字段
//                foreignField("foodid").//从表关联的字段
//                as("foodNumber");   //查询结果名
        Criteria criteria = new Criteria();
        criteria.andOperator(
                Criteria.where("shopid").is(shopid),
                Criteria.where("enable").is(1)
        );
        //将筛选条件放入管道中
//        MatchOperation match = Aggregation.match(criteria);
//        Aggregation agg = Aggregation.newAggregation(lookup, match);

        Query query=new Query();
        query.addCriteria(criteria);

        List<Food> mgt =  mongoTemplate.find(query , Food.class);
        if(mgt==null)return new ArrayList<>();
        return mgt;
//        try {
//            AggregationResults<Food> studentAggregation = mongoTemplate.aggregate(agg, "food", Food.class);
//            System.out.println(studentAggregation.getMappedResults().toString());
//            return (studentAggregation.getMappedResults());
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new ArrayList<>();
//        }


    }

    /**
     * 搜索商店所有的优惠内容
     * @param shopid
     * @return
     */
    public ShopPreferential selectPreferentialByShopId(Integer shopid) {
        Query query=new Query(Criteria.where("shopid").is(shopid));
        ShopPreferential mgt =  mongoTemplate.findOne(query , ShopPreferential.class);
        return mgt;
    }
}
